import {
  getGoodServiceTable,
  editGoodServiceRecord,
  deleteGoodServiceRecord,
  getGoodsTable
} from '../../api/explosion'
import {
  GOOD_SERVICE_LIST,
  GOODS_LIST,
  GOOD_SERVICE_CURRENT_ROW,
  GOOD_SERVICE_DATA_COUNT,
  GOOD_SERVICE_HISTORY_DATA,
  GOOD_SERVICE_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    goodServiceList: [],
    goodsList: [],
    goodServiceDataCount: null,
    goodServicePageSize: 20,
    goodServiceHistoryData: [],
    row: {
      name: '',
      ctime: '',
      state: '',
      des: '',
      price: '',
      openState: false
    }
  },
  getters: {
    goodServiceList: state => state.goodServiceList
  },
  mutations: {
    [GOOD_SERVICE_LIST] (state, data) {
      state.goodServiceList = data
    },
    [GOODS_LIST] (state, data) {
      state.goodsList = data
    },
    [GOOD_SERVICE_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [GOOD_SERVICE_DATA_COUNT] (state, data) {
      state.goodServiceDataCount = data
    },
    [GOOD_SERVICE_HISTORY_DATA] (state, data) {
      state.goodServiceHistoryData = data
    },
    [GOOD_SERVICE_PAGE_SIZE] (state, data) {
      state.goodServicePageSize = data
    }
  },
  actions: {
    async getGoodServiceList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getGoodServiceTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          name: item.name,
          ctime: item.ctime,
          state: item.state,
          des: item.des,
          price: item.price,
          openState: item.state === 0
        }]
      })
      commit(GOOD_SERVICE_LIST, newData)
      commit(GOOD_SERVICE_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.goodService.goodServicePageSize) {
        commit(GOOD_SERVICE_HISTORY_DATA, newData)
      } else {
        commit(GOOD_SERVICE_HISTORY_DATA, newData.slice(0, rootState.goodService.goodServicePageSize))
      }
      return resp
    },
    async getGoodsList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getGoodsTable(data)
      // console.log(resp)
      resp.data.data.map((item) => {
        newData = [...newData, {
          id: item.id,
          name: item.name
        }]
      })
      commit(GOODS_LIST, newData)
      return resp
    },
    async editGoodServiceData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editGoodServiceRecord(data)
      return resp
    },
    async deleteGoodServiceData ({ commit, rootState }, playload) {
      await deleteGoodServiceRecord(playload)
    },
    async getGoodServiceCurrentRow ({ commit, rootState }, row) {
      commit(GOOD_SERVICE_CURRENT_ROW, row)
    },
    async changeGoodServicePage ({ commit, rootState }, index) {
      let data = rootState.goodService.goodServiceList
      let _start = (index - 1) * rootState.goodService.goodServicePageSize
      let _end = index * rootState.goodService.goodServicePageSize
      commit(GOOD_SERVICE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeGoodServicePageSize ({ commit, rootState }, value) {
      commit(GOOD_SERVICE_PAGE_SIZE, value)
    }
  }
}
